package liu.zhi.peng.ui;

import javax.imageio.plugins.tiff.TIFFImageReadParam;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

public class GameJFrame extends JFrame implements ActionListener {
    int i=-1;
    int j=0;
    int sumDinner=9;
    Font font=null;
    boolean flagShake = true;
    boolean flagNew = false;
    String [] arr ={"黄焖鸡米饭","面世家","炒饭/面","麻辣烫","腊肠饭","韩式滑蛋饭","麻辣拌","自选菜","螺蛳粉"};
    JButton button1 = new JButton(new ImageIcon("D:\\java-code\\eat_rice\\src\\运行.png"));
    JButton button2 = new JButton(new ImageIcon("D:\\java-code\\eat_rice\\src\\退出.png"));
    JButton button3 = new JButton(new ImageIcon("D:\\java-code\\eat_rice\\src\\重来.png"));
    public GameJFrame(){
        myCreatFont();
        initJFrame();
        button1.addActionListener(this);
        button2.addActionListener(this);
        button3.addActionListener(this);
        time3();
    }
    private void time3() {
        for(j=0;j<3;j++)
        {
            flagShake=true;
            repaint();
            while(!flagNew){
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    public void repaint() {
            while (flagShake){
                initImage();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
    }

    public void initJFrame(){
        this.setTitle("干饭程序");
        this.setSize(1200,700);
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(3);
        this.setLayout(null);
    }
    public void initImage(){
        this.getContentPane().removeAll();
        if(flagShake)
            i++;
        //菜品名称
        JLabel jLabel = new JLabel(arr[i%sumDinner],SwingConstants.CENTER);
        jLabel.setFont(font.deriveFont(80f));
        jLabel.setBounds(320,150,500,150);
        this.getContentPane().add(jLabel);

        button1.setBounds(170,380,200,200);
        //去除按钮默认边框
        button1.setBorderPainted(false);
        //去除按钮背景
        button1.setContentAreaFilled(false);

        button2.setBounds(750,380,200,200);
        button2.setBorderPainted(false);
        button2.setContentAreaFilled(false);

        button3.setBounds(170,380,200,200);
        //去除按钮默认边框
        button3.setBorderPainted(false);
        //去除按钮背景
        button3.setContentAreaFilled(false);
        if(flagShake)
        this.getContentPane().add(button1);
        else
            this.getContentPane().add(button3);
        this.getContentPane().add(button2);
//        两种不同的刷新方式，侧重点不同
//        this.getContentPane().revalidate();
        this.getContentPane().repaint();
        this.setVisible(true);
        System.out.println(i);
    }

    private void myCreatFont() {
        File file = new File("D:\\java-code\\eat_rice\\lib\\字体1\\像素字体.otf");
        try {
             font = Font.createFont(Font.TRUETYPE_FONT,file);
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            ge.registerFont(font);
        } catch (FontFormatException e) {
            System.out.println("字体加载失败");
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (IOException e) {
            System.out.println("字体文件不存在");
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        if(obj==button1){
            System.out.println("点击了按钮");
            flagShake=false;
            flagNew = false;
            initImage();
        }
        else if(obj==button2){
            System.exit(0);
        }
        else if(obj==button3){
            flagShake=true;
            flagNew = true;
        }
    }
}